package controllers;

import models.AccNameMap;
import models.TimeLog;
import models.Login;
import play.data.Form;
import play.mvc.*;
import views.html.*;

public class UserAdmin extends Controller {

	// -- Authentication

	/**
	 * Login page.
	 */
	public static Result login() {

		return ok(login.render(Form.form(Login.class)));
	}

	public static Result authenticate() {

		Form<Login> loginForm = Form.form(Login.class).bindFromRequest();
		if (loginForm.hasErrors()) {
			return badRequest(index.render("Login failed! Please check the active domain account and password."));
		}
		session("connected", AccNameMap.map().get(loginForm.get().userId).get());
		return ok(timelog.render(session("connected"),Form.form(TimeLog.class)));
	}

	public static Result logout() {
		session().clear();
		flash("success", "You've been logged out");
		return redirect(routes.UserAdmin.login());
	}
}
